//
//  MDProvince.m
//  evidenceGather
//
//  Created by 王继伟 on 13-8-27.
//  Copyright (c) 2013年 无. All rights reserved.
//

#import "MDProvince.h"
#import "ProviceVO.h"

@implementation MDProvince

//数据库插入记录对象
-(NSString *)insertProvinceVO:(ProviceVO *) provinceVO{
    
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"insert into province(prvcode,prvname,loginuser) values('%@','%@','%@');",provinceVO.prvcode,provinceVO.prvname,provinceVO.loginuser];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL];
    return provinceVO.prvcode;
    
}


-(void *)updateProvinceClsVO:(ProviceVO *) provinceVO{
    NSString *insertSQL=[[NSString alloc] initWithFormat:@"update province set loginuser='%@',prvname='%@' where prvcode='%@'" ,provinceVO.loginuser,provinceVO.prvname,provinceVO.prvcode];
    //NSLog(@"the insert sql is %@",insertSQL);
    [super.dbDelegate executeUpdate:insertSQL];
    
}


//新增或者更新问题分类信息
-(void *)update:(NSArray *) projclsVOs{
    if(projclsVOs!=nil){
        int count=[projclsVOs count];
        for (int i=0;i<count;i++) {
            ProviceVO *provinceVO= [projclsVOs objectAtIndex:i];
            ProviceVO *tempProviceVO= [self qryProvinceByPk:provinceVO.prvcode];
            //如果不存在，则新增
            if(tempProviceVO==nil){
                [self insertProvinceVO:provinceVO];
            }else{
                [self updateProvinceClsVO:provinceVO];
            }
        }
    }
}

//根据用户查询项目类别
-(ProviceVO *)qryProvinceByPk:(NSString *) prvcode{
    NSString *sqlWhere=@" prvcode='";
    sqlWhere=[sqlWhere stringByAppendingString:prvcode];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    NSMutableDictionary *dic=[self qryProvinceBySqlWhere:sqlWhere];
    if(dic!=nil){
        return [dic objectForKey:prvcode];
    }
    return nil;
    
}

//根据用户查询项目
-(NSMutableDictionary *)qryProvinceByPkUser:(NSString *) pkUser{
    NSString *sqlWhere=@" loginuser='";
    sqlWhere=[sqlWhere stringByAppendingString:pkUser];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    return [self qryProvinceBySqlWhere:sqlWhere];
    
    
}

//根据省份查询项目
-(NSMutableDictionary *)qryProvinceByPkPrvcode:(NSString *) prvcode{
    NSString *sqlWhere=@" prvcode='";
    sqlWhere=[sqlWhere stringByAppendingString:prvcode];
    sqlWhere=[sqlWhere stringByAppendingString:@"'"];
    return [self qryProvinceBySqlWhere:sqlWhere];

    
    
}

//根据条件查询项目
-(NSMutableDictionary *)qryProvinceBySqlWhere:(NSString *) sqlWhere{
    NSMutableDictionary *result = nil;
    if(nil != sqlWhere && [sqlWhere length] > 0)
    {
        NSMutableString *sql = [[NSMutableString alloc]initWithString:@"select prvcode,prvname,loginuser from province where"];
        [sql appendString:sqlWhere];
        //执行查询语句
        NSArray *rs=[super.dbDelegate executeQuery:sql];
        if(nil != rs && [rs count] > 0){
            //如果查询结果不为空，则初始化dictionary，并循环赋值
            result = [[NSMutableDictionary alloc]init];
            for(int i=0;i < [rs count]; i++){
                NSDictionary *dic = [rs objectAtIndex:i];
                //获取附件对应的主键
                NSString *key = [[NSString alloc] initWithData:[dic objectForKey:@"prvcode"] encoding:NSUTF8StringEncoding];
                ProviceVO *projectVO = [result objectForKey:key];
                if(nil == projectVO)   //当结果集内无对应的Bug记录时
                {
                    projectVO = [[ProviceVO alloc]init];
                    projectVO.prvcode = key;
                    projectVO.prvname =[[NSString alloc] initWithData:[dic objectForKey:@"prvname"] encoding:NSUTF8StringEncoding];
                    projectVO.loginuser = [[NSString alloc] initWithData:[dic objectForKey:@"loginuser"] encoding:NSUTF8StringEncoding];
                    [result setObject:projectVO forKey:key];
                }
            }
        }
    }
    return result;
}
@end
